package yunquan.wang.modules.base.service.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import yunquan.wang.base.dao.springdata.BaseRepository;
import yunquan.wang.base.report.ReportBuildUtils;
import yunquan.wang.base.service.impl.BaseServiceImpl;
import yunquan.wang.modules.base.dao.DynamicColumnDao;
import yunquan.wang.modules.base.dao.DynamicReportDao;
import yunquan.wang.modules.base.entity.DynamicColumn;
import yunquan.wang.modules.base.entity.DynamicReport;
import yunquan.wang.modules.base.service.DynamicReportService;

@Service("dynamicReportServiceImpl")
@Transactional(value = "jpaTransaction")
public class DynamicReportServiceImpl extends BaseServiceImpl<DynamicReport>
		implements DynamicReportService {

	@Resource
	private DynamicReportDao dynamicReportDao;
	@Resource
	private DynamicColumnDao dynamicColumnDao;
	@Resource
 DataSource dataSource;

	@Override
	public BaseRepository<DynamicReport, String> getDao() {
		return dynamicReportDao;
	}

	@Override
	public List<DynamicColumn> getQueryColumn(String reportId) {

		List<DynamicColumn> columns=new ArrayList<DynamicColumn>();
		DynamicReport report = dynamicReportDao.findOne(reportId);
		String sqlType = report.getSqlType();
		if (sqlType.equalsIgnoreCase("JDBC")) {
			String sql=report.getSqlString();
			sql=subWhereSql(sql);
			Connection conn=null;
			PreparedStatement  ps=null;
			ResultSet rs=null;
			try {
			 conn=	dataSource.getConnection();
		   ps=		conn.prepareStatement(sql);
		 rs=	ps.executeQuery();
		ResultSetMetaData rsmd = rs.getMetaData();
		int columnCount = rsmd.getColumnCount();
		for (int i = 1; i <= columnCount; i++) {
		String columnName=	JdbcUtils.lookupColumnName(rsmd, i);
		Integer type=rsmd.getColumnType( i );
		DynamicColumn dm=new DynamicColumn();
		dm.setName(columnName);
		dm.setType(type.toString());
		columns.add(dm);
		}
			} catch ( Exception e) {
				e.printStackTrace();
			}
			finally
			{
				JdbcUtils.closeResultSet(rs);
				JdbcUtils.closeStatement(ps);
				JdbcUtils.closeConnection(conn);
			}

		}  
		return columns;
	}

	private String subWhereSql(String sql)
	{
		String sqltoChange=sql.toUpperCase();
		if(sqltoChange.contains("WHERE"))
		{
			int index=sqltoChange.indexOf("WHERE");
			sql=sql.substring(0,index)	;
		}
	 return sql;
	}

	@Override
	public DynamicReport generateColumn(DynamicReport entity,
			List<DynamicColumn> columnList) throws Exception {
		 
		dynamicReportDao.save(entity);
		//TODO  报表文件保存路径
		entity.setFileUrl("D:\\entity.xml");
		for(DynamicColumn column:columnList)
		{
			column.setDynamicReport(entity);
			dynamicColumnDao.save(column);
			entity.getColumns().add(column);
		}
		ReportBuildUtils.buildReport(entity);
		return entity;
	}
}
